<?php
namespace app\admin\model;
use think\Model;
use think\Db;
class Notice extends Model
{
    function noticeData($size=15,$where=''){
        $map[] = ['n.is_del','=','0'];
        if(input('keyword') && input('keyword')<>''){
            $map[] = ['n.title','like','%'.input('keyword').'%'];
        }
        if($where <> ''){
            $map = array_merge($where,$map);
        }
        $count = Db::name('notice')->alias('n')->where($map)->count();// 查询满足要求的总记录数
        $list = Db::name('notice')
            ->alias('n')
//            ->join('subject s','e.subjectid = s.subjectid','left')
//            ->field('e.*,s.subject_name')
            ->where($map)
            ->order('noticeid desc')
            ->paginate($size,$count,['query'=>request()->param()])
            ->each(function($item,$key){
                return $item;
            });
        $page = $list->render();

        $data['count'] = $count;
        $data['list'] = $list;
        $data['page'] = $page;
        return $data;
    }
    function getTesting($id){
        if($id > 0){
            $testing = Db::name('testing')->json(['testing_set'])->find($id);
            return $testing;
        }else{
            exitjson(0,'参数错误');
        }

    }
    //获取各科的试卷
    function getExamInfo()
    {
        $examList = Db::name('exam')->where('is_del=0')->order('examid desc')->select();
        $subjectList = Db::name('subject')->order('subjectid desc')->select();
        foreach($subjectList as $k=>$v){
            $subject_list[$v['subjectid']] = $v;
        }
        foreach($examList as $k=>$v){
            $subject_list[$v['subjectid']]['examList'][$v['examid']] = $v;
        }
        return $subject_list;
    }

    //处理参数
    function disposeData($data,$act=''){
        $newData['testing_name'] = $data['testing_name'];
        $newData['short_name'] = $data['short_name'];
        if($act == 'add'){
            foreach($data as $k=>$v){
                if(strpos($k,'subject')!==false && $v<>""){
                    $subjectid = substr($k,8);
                    $testingInfo[$subjectid] = $v;
                }
            }
            $newData['testing_set'] = json_encode($testingInfo);

            $newData['admin_id'] = session('admin_id');
            $newData['add_name'] = session('user_name');
            $newData['add_time'] = time();
        }

        return $newData;
    }
}